package org.song.controller;

import com.github.pagehelper.PageHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.song.annotation.LogRecord;
import org.song.annotation.PowerEquals;
import org.song.annotation.RequestTimeRecord;
import org.song.model.dto.UserDto;
import org.song.exception.result.AjaxResult;
import org.song.model.entity.UserBlogOperateEntity;
import org.song.model.vo.User;
import org.song.service.UserService;
import org.song.service.cache.CaptchaRedisCacheService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.lang.NonNull;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

@Api(tags = "用户控制层")
@Controller
@RequestMapping(value = "/user")
public class UserController {

    @Autowired( required = true )
    @Qualifier( value = "userServiceImpl")
    private UserService userService;

    @Autowired( required = true )
    @Qualifier( value = "captchaRedisCacheServiceImpl")
    private CaptchaRedisCacheService captchaRedisCacheService;

    /**
     * 查询所有的用户
     * @param userDto
     * @return
     */
    @RequestTimeRecord
    @ApiOperation(value = "查询所有的用户" , notes = "查询所有的用户")
    @ResponseBody
    @RequestMapping(value = "/listUsers" , method = RequestMethod.GET)
    public AjaxResult<List<User>> listUsers(@ApiParam(value = "userDto实例接受查询参数") @NonNull UserDto userDto) {
        PageHelper.startPage(userDto.getPage() , userDto.getSize());
       return AjaxResult.success(userService.listUsers(userDto));
    }

    /**
     * 删除用户
     * @param user
     * @return
     */
    @RequestTimeRecord
    @LogRecord
    @ApiOperation(value = "删除用户" , notes = "删除用户")
    @ResponseBody
    @RequestMapping(value = "/deleteUser" , method = RequestMethod.DELETE)
    public AjaxResult<Integer> deleteUser(@ApiParam(value = "接受需要删除的用户") @NonNull @RequestBody User user) {
        return AjaxResult.success(userService.deleteUser(user));
    }

    /**
     * 增加用户
     * @param user
     * @return
     */
    @RequestTimeRecord
    @LogRecord
    @ApiOperation(value = "增加用户" , notes = "增加用户")
    @ResponseBody
    @RequestMapping(value = "/insertUser" , method = RequestMethod.PUT)
    public AjaxResult<Integer> insertUser(@ApiParam(value = "用户") @NonNull @RequestBody User user) {
        return AjaxResult.success(userService.insertUser(user));
    }

    /**
     * 更新用户
     * @param user
     * @return
     */
    @RequestTimeRecord
    @LogRecord
    @ApiOperation(value = "更新用户" , notes = "更新用户")
    @ResponseBody
    @RequestMapping(value = "/updateUser" , method = RequestMethod.POST)
    public AjaxResult<Integer> updateUser(@ApiParam(value = "根据传入的用户信息进行用户更新") @NonNull @RequestBody User user) {
        return AjaxResult.success(userService.updateUser(user));
    }

    /**
     * 添加用户博客操作记录
     * @param userBlogOperateEntity
     * @return
     */
    @PowerEquals
    @RequestTimeRecord
    @LogRecord
    @ApiOperation(value = "添加用户博客操作记录" , notes = "添加用户博客操作记录")
    @ResponseBody
    @RequestMapping(value = "/insertUserBlogOperate" , method = RequestMethod.POST)
    public AjaxResult<Integer> insertUserBlogOperate(@ApiParam( value = "用户博客操作实体接受") @NonNull @RequestBody UserBlogOperateEntity userBlogOperateEntity ) {
        return AjaxResult.success(userService.insertUserBlogOperate(userBlogOperateEntity));
    }

    /**
     * 获取用户博客操作记录
     * @param userBlogOperateEntity
     * @return
     */
    @PowerEquals
    @RequestTimeRecord
    @ApiOperation(value = "获取用户博客操作记录" , notes = "获取用户博客操作记录")
    @ResponseBody
    @RequestMapping(value = "/getUserBlogOperate" , method = RequestMethod.GET)
    public AjaxResult<Integer> whetherOperateBlog(@ApiParam( value = "用户博客操作实体接受") @NonNull UserBlogOperateEntity userBlogOperateEntity ) {
        return AjaxResult.success(userService.getUserBlogOperate(userBlogOperateEntity));
    }

    /**
     * 删除用户博客操作记录
     * @param userBlogOperateEntity
     * @return
     */
    @PowerEquals
    @RequestTimeRecord
    @LogRecord
    @ApiOperation(value = "删除用户博客操作记录" , notes = "删除用户博客操作记录")
    @ResponseBody
    @RequestMapping(value = "/deleteUserBlogOperate" , method = RequestMethod.DELETE)
    public AjaxResult<Integer> deleteUserBlogOperate(@ApiParam( value = "用户博客操作实体接受") @NonNull UserBlogOperateEntity userBlogOperateEntity ) {
        return AjaxResult.success(userService.deleteUserBlogOperate(userBlogOperateEntity));
    }

    /**
     * 用户登录
     * @param username
     * @param password
     * @param captchaCode
     * @param uuId
     * @return
     */
    @PowerEquals
    @RequestTimeRecord
    @LogRecord
    @ApiOperation(value = "用户登录" , notes = "用户登录")
    @ResponseBody
    @RequestMapping(value = "/userLogin" , method = RequestMethod.GET)
    public AjaxResult<User> userLogin( @ApiParam( value = "用户名" ) @RequestParam( value = "username" , required = true ) String username ,
                                          @ApiParam( value = "密码" ) @RequestParam( value = "password" , required = true ) String password ,
                                          @ApiParam( value = "验证码" ) @RequestParam( value = "captchaCode" , required = true ) String captchaCode,
                                          @ApiParam( value = "uuId" ) @RequestParam( value = "uuId" , required = true ) String uuId) {
        // TODO:临时登录接口 后续待改 springSecurity 管理 逻辑判断暂时在控制层处理
        final User user = CollectionUtils.firstElement(userService.listUsers(UserDto.builder().username(username).password(password).build()) );
        if (  captchaCode.equalsIgnoreCase( captchaRedisCacheService.getCaptcha( uuId ))
                && Objects.nonNull( user ) ) {
            return AjaxResult.success(user);
        }
        return AjaxResult.failed();
    }

    /**
     * 判断用户是否首次访问
     * @return
     */
    @RequestMapping( value = "/firstVisit" , method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation( value = "判断用户是否首次访问")
    public AjaxResult<Boolean> firstVisit() {
        return AjaxResult.success(userService.firstVisit());
    }

}
